package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivitySyncTimeBinding;
import com.apex.aolonfitdemo.dialog.HourSystemDialog;
import com.apex.aolonfitdemo.dialog.MonthDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.TimeCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.enumeration.TimeZone;
import com.apex.ax_bluetooth.model.EABleSyncTime;
import com.cyber.bridge.module.listener.OnCyberQueryAndSetStateListener;

import java.util.Calendar;


public class SyncTimeActivity extends AppCompatActivity {

    private WaitingDialog waitingDialog;


    private HourSystemDialog hourSystemDialog;


    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                if (msg.arg1 == 2) {
                    activitySyncTimeBinding.hourSystem.setText(getString(R.string.twelve_hour_system));
                } else {
                    activitySyncTimeBinding.hourSystem.setText(getString(R.string.twenty_four_hour_system));
                }

            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(SyncTimeActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivitySyncTimeBinding activitySyncTimeBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activitySyncTimeBinding = ActivitySyncTimeBinding.inflate(getLayoutInflater());

        setContentView(activitySyncTimeBinding.getRoot());
        activitySyncTimeBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activitySyncTimeBinding.tool);
        getSupportActionBar().setTitle(null);
        activitySyncTimeBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnTimeFormatListener(new OnCyberQueryAndSetStateListener<Integer>() {
            @Override
            public void queryConfig(Integer integer) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.arg1 = integer;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setConfig(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x41 : 0x42);
                }
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(SyncTimeActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryTimeFormat();


        activitySyncTimeBinding.hourSystem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hourSystemDialog == null) {
                    hourSystemDialog = new HourSystemDialog(SyncTimeActivity.this);
                    hourSystemDialog.setSelectListener(new HourSystemDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activitySyncTimeBinding.hourSystem.setText(sex);
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(SyncTimeActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            AolonFitManager.getInstance().setTimeFormat(sex.equalsIgnoreCase(getString(R.string.twelve_hour_system)) ? 2 : 1);
                        }
                    });
                }
                if (!hourSystemDialog.isShowing()) {
                    hourSystemDialog.show();
                }

            }
        });

    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }

        if (hourSystemDialog != null) {
            hourSystemDialog.dismiss();
            hourSystemDialog.destroyDialog();
            hourSystemDialog = null;
        }

        super.onDestroy();
    }
}
